
import Tools from "@/CTManage/utils/Tools";
import LogUtils from "@/CTManage/utils/LogUtils";

const DateUtils = {

  name: 'DateUtils',

  DEFAULT_DATE_SPILT_CHAR : '-',

  ONE_DAY_MILLISECOND: 86400000,
  DAY: 'day',

  /* 获取日期，无格式 */
  getDate(date) {

    if(!(date instanceof Date)) {
      date = new Date();
    }

    return Tools.strFill(date.getFullYear(), 0, -4)
      + Tools.strFill(date.getMonth() + 1, 0, -2)
      + Tools.strFill(date.getDate(), 0, -2)
    ;
  },

  /* 获取时间，无格式 */
  getTime(date) {

    if(!(date instanceof Date)) {
      date = new Date();
    }

    return Tools.strFill(date.getHours(), 0, -2)
      + Tools.strFill(date.getMinutes(), 0, -2)
      + Tools.strFill(date.getSeconds(), 0, -2)
    ;
  },

  /* 获取日期时间，无格式 */
  getDateTime(date) {

    if(!(date instanceof Date)) {
      date = new Date();
    }

    return this.getDate(date) + this.getTime(date);
  },

  conventStr2Time(str) { /* 转换时间字符串为日期对象 120101/12:01:01 */
    let methodName = this.name + 'comventStr2Date: ';
    let debug = false;

    if(Tools.isNull(str)
      || typeof str != 'string'
    ) {
      return null;
    }

    let len = (str = str.trim()).length;

    if(len == 10) {

      if(str.indexOf(this.DEFAULT_DATE_SPILT_CHAR) == -1
        && str.indexOf('.') == -1
        && str.indexOf('/') == -1
      ) {
        return null;
      }
    } else if(len == 8) {

      let year = str.substring(0, 4);
      let month = str.substring(4, 6);
      let day = str.substring(6, 8);

      str = year + this.DEFAULT_DATE_SPILT_CHAR
        + month + this.DEFAULT_DATE_SPILT_CHAR
        + day;

    } else {
      return null;
    }

    return new Date(str);
  },

  conventStr2Date(str) { /* 转换日期字符串为日期对象 20190101/2019-01-01/2019.01.01 */
    let methodName = this.name + 'comventStr2Date: ';
    let debug = false;

    if(Tools.isNull(str)
      || typeof str != 'string'
    ) {
      return null;
    }

    let len = (str = str.trim()).length;

    if(len == 10) {

      if(str.indexOf(this.DEFAULT_DATE_SPILT_CHAR) == -1
        && str.indexOf('.') == -1
        && str.indexOf('/') == -1
      ) {
        return null;
      }
    } else if(len == 8) {

      let year = str.substring(0, 4);
      let month = str.substring(4, 6);
      let day = str.substring(6, 8);

      str = year + this.DEFAULT_DATE_SPILT_CHAR
        + month + this.DEFAULT_DATE_SPILT_CHAR
        + day;

    } else {
      return null;
    }

    return new Date(str);
  },

  between(date1, date2, type) {
    let methodName = 'between: ';
    let debug = false;

    if(debug) {
      LogUtils.debug(date1, methodName + 'date1');
      LogUtils.debug(date2, methodName + 'date2');
      LogUtils.debug(type, methodName + 'type');
    }

    if(Tools.isNull(date1)
      || Tools.isNull(date2)
    ) {
      if(debug) {
        LogUtils.debug(methodName + '参数传递错误');
      }
      return -1;
    }

    if(typeof date1 == 'string') {
      date1 = this.conventStr2Date(date1);
    }
    if(typeof date2 == 'string') {
      date2 = this.conventStr2Date(date2);
    }

    if(!(date1 instanceof Date)
       || !(date2 instanceof Date)
    ) {
      LogUtils.debug(methodName + 'date1 或 date2 非 Date 类型，转换失败');
      return -1;
    }

    let result = date2 - date1;
    if(debug) {
      LogUtils.debug(result, methodName + 'result');
    }
    switch(type) {
      case this.DAY:
        return parseInt(result / this.ONE_DAY_MILLISECOND);
    }

    return result; /* 默认返回毫秒数 */
  },

  betweenWithDetail(param){ // 获取两个时间之间的间隔，细分到秒
    let methodName = this.name + '.betweenWithDetail: ';

    if(Tools.isNull(param)) {
      LogUtils.debug(methodName + '参数未传递');
      return;
    }

    let date1 = Tools.getValue(param, 'date1');
    let date2 = Tools.getValue(param, 'date2');
    let debug = Tools.getValue(param, 'debug');

    if(!(date1 instanceof Date)
      || !(date2 instanceof Date)
    ) {
      if(debug) {
        LogUtils.debug(methodName + '参数传递错误');
      }
      return null;
    }

    let between = {};

    let times = date2 - date1;
    if(debug) {
      LogUtils.debug(times, methodName + '毫秒');
    }

    times = parseInt(times / 1000);
    if(debug) {
      LogUtils.debug(times, methodName + '毫秒');
      console.log('秒：' + times);
    }

    let second = times % 60
    times = parseInt(times / 60);
    Tools.setValue(between, 'second', second);
    if(debug) {
      LogUtils.debug(second, methodName + '余秒');
      LogUtils.debug(times, methodName + '分');
    }

    let minute = times % 60;
    times = parseInt(times / 60);
    Tools.setValue(between, 'minute', minute);
    if(debug) {
      LogUtils.debug(minute, methodName + '余分');
      LogUtils.debug(times, methodName + '时');
    }

    let hour = times % 24;
    times = parseInt(times / 24);
    Tools.setValue(between, 'hour', hour);
    if(debug) {
      LogUtils.debug(hour, methodName + '余时');
      LogUtils.debug(times, methodName + '天');
    }

    Tools.setValue(between, 'day', times);

    if(debug) {
      LogUtils.debug(between, methodName + 'between');
    }

    return between;
  },

  getThisMonthFirstDay() { // 获取本月起始时间
    let date = new Date();
    date.setDate(1);
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    return date;
  },

  getThisMonthLastDay() { // 获取本月结束时间
    let date = this.getThisMonthFirstDay();
    date.setMonth(date.getMonth() + 1);
    date.setSeconds(-1);
    return date;
  }
}

export default DateUtils;
